<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    //首页
    public function index()
    {
        $this->assign("title", "首页");
        $this->assign("foot_nav_index", "1");

        $user = $this->auth->getUser();
        if ($user) {
            $user["avatar"] = $user["avatar"] ? $user["avatar"] : letter_avatar($user["nickname"]);
        }
        $this->assign("user", $user);

        //首页-轮播图
        $head_ad_list = db("article")->where("category_id", 11)->select();
        $this->assign("head_ad_list", $head_ad_list);


        //推荐的公告
        $notice = db("article")->where("category_id", 5)->orderRaw("is_recommend desc,id desc")->find();
        $this->assign("notice", $notice);

        //视频号学院-文章
        $article_list = db("article a")->join("article_category c", "a.category_id=c.id", "LEFT")->where("a.category_id", 17)->field("a.*,c.title as category_name")->order("weigh asc")->limit(2)->select();
        $this->assign("article_list", $article_list);

        //积分商城-推荐的商品
        $where['upper'] = 1;
        $where['one_type_id'] = 1;
        $where['is_recommend'] = 1;
        $recommand_goods_list = Db::name('mini_goods')->field('id,name,one_type_id,min_price,min_use_coupon_price,img')->where($where)->order('id desc')->limit(4)->select();
        if ($recommand_goods_list) {
            $recommand_goods_list = addOtherTableDataToList($recommand_goods_list, 'img', Db::name('mini_picture'), 'img_path', 'path', 'id');
            foreach ($recommand_goods_list as &$v) {
                $v['img'] = doXcxImgPath($v['img_path']);
                $v['price_info'] = doPrice($v['min_price']);
                $v['min_use_coupon_price'] = intval($v['min_use_coupon_price']);
            }
        }
//        echo "<pre>";
//        print_r($recommand_goods_list);
//        exit;
        $this->assign("recommand_goods_list", $recommand_goods_list);


        return $this->view->fetch();
    }

    /*
     * 服务协议 
     */
    public function contract()
    {
        $type = input("type", 0); //0服务协议 ;1隐私协议
        if ($type == 0) {
            $id = 30; //服务协议
        } else {
            $id = 31; //1隐私协议
        }

        $article = db("article")->where("id", $id)->find();
        // exit(print_r($article));
        if ($article) {
            //            db("article")->where("id", $id)->setInc("read_count",1);
            $article["createtime_format"] = date("Y年m月d日 H:i", $article["createtime"]);
        }
        $this->assign("title", $article['title']);
        $this->assign("article", $article);

        return $this->view->fetch();
    }

    /*
     * 
     * 
     */
}
